package xdu.lz.stage2_design_pattern.chapter6_immutable_pattern.performance_test;

final class ImmutableObj{
    private final String name;

    ImmutableObj(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "ImmutableObj{" +
                "name='" + name + '\'' +
                '}';
    }
}

class SynchronizedObj{
    private String name;

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public synchronized String toString() {
        return "SynchronizedObj{" +
                "name='" + name + '\'' +
                '}';
    }
}
public class ImmutablePerformanceTest {
    public static void main(String[] args) throws InterruptedException {
        final int COUNT = 1000000;
        long startTime = System.currentTimeMillis();
        SynchronizedObj synchronizedObj = new SynchronizedObj();
        ImmutableObj immutableObj = new ImmutableObj("zs");
        synchronizedObj.setName("zs");

        Thread t1 = new Thread(()->{
            for (int i = 0; i < COUNT; i++) {
                System.out.println(synchronizedObj.toString()); // use time 13840 ms
            }
        });
        Thread t2 = new Thread(()->{
            for (int i = 0; i < COUNT; i++) {
                System.out.println(synchronizedObj.toString());
            }
        });
        Thread t11 = new Thread(()->{
            for (int i = 0; i < COUNT; i++) {
                System.out.println(synchronizedObj.toString());
            }
        });
        Thread t21 = new Thread(()->{
            for (int i = 0; i < COUNT; i++) {
                System.out.println(synchronizedObj.toString());
            }
        });
        t1.start();
        t11.start();
        t2.start();
        t21.start();

        t1.join();
        t11.join();
        t2.join();
        t21.join();

        long time1 = System.currentTimeMillis();

        Thread t3 = new Thread(()->{
            for (int i = 0; i < COUNT; i++) {
                System.out.println(immutableObj.toString());// use time 12643 ms
            }
        });
        Thread t4 = new Thread(()->{
            for (int i = 0; i < COUNT; i++) {
                System.out.println(immutableObj.toString());
            }
        });
        Thread t31 = new Thread(()->{
            for (int i = 0; i < COUNT; i++) {
                System.out.println(immutableObj.toString());// use time 12643 ms
            }
        });
        Thread t41 = new Thread(()->{
            for (int i = 0; i < COUNT; i++) {
                System.out.println(immutableObj.toString());
            }
        });
        t3.start();
        t31.start();
        t41.start();
        t4.start();
        t3.join();
        t31.join();
        t4.join();
        t41.join();

        long time2 = System.currentTimeMillis();
        System.out.println("耗时:"+(time1 - startTime));
        System.out.println("耗时:"+(time2 - time1));

    }
}
